package com.infosys.setl.fp.demo6;

import org.apache.commons.functor.UnaryPredicate;
import org.apache.commons.functor.core.composite.UnaryAnd;
import org.apache.commons.functor.core.composite.UnaryNot;
import org.apache.commons.functor.core.composite.UnaryOr;

public class SetOps {
    //并集
    public static <A> UnaryPredicate<A> union(UnaryPredicate<A> up1, UnaryPredicate<A> up2) {
        return new UnaryOr<A>().or(up1).or(up2);
    }

    //交集
    public static <A> UnaryPredicate<A> intersection(UnaryPredicate<A> up1, UnaryPredicate<A> up2) {
        return new UnaryAnd<A>().and(up1).and(up2);
    }

    //两个集合的差
    public static <A> UnaryPredicate<A> difference(UnaryPredicate<A> up1, UnaryPredicate<A> up2) {
        return new UnaryAnd<A>().and(up1).and(new UnaryNot<A>(up2));
    }

    //两个集合的对称差
    public static <A> UnaryPredicate<A> symmetricDifference(UnaryPredicate<A> up1, UnaryPredicate<A> up2) {
        return difference(union(up1, up2), intersection(up1, up2));
    }
}